package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.dao.BandPointDao;
import com.bestcem.xm.award.dao.mapper.BandPointMapper;
import com.bestcem.xm.award.entity.mysql.BandPointEntity;
import com.bestcem.xm.award.entity.pojo.BandPointDO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 需要加唯一索引或者加锁
 *
 * @author xa.zhang <xa.zhangi@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/8 14:28
 */
@Repository
public class BandPointDaoImpl implements BandPointDao {

    @Resource
    private BandPointMapper bandPointMapper;

    @Override
    public BandPointDO selectByPrimaryKey(String id) {
        BandPointEntity entity = bandPointMapper.selectByPrimaryKey(id);
        if (entity == null) {
            return null;
        }
        BandPointDO bandPointDO = new BandPointDO();
        BeanUtils.copyProperties(entity, bandPointDO);

        return bandPointDO;
    }

    @Override
    public BandPointDO findByNameNotDel(String name, String orgId) {
        BandPointEntity entity = bandPointMapper.findByNameNotDel(name, orgId);
        if (entity == null) {
            return null;
        }
        BandPointDO bandPointDO = new BandPointDO();
        BeanUtils.copyProperties(entity, bandPointDO);

        return bandPointDO;
    }

    @Override
    public BandPointDO findByCode(String code, String orgId) {
        BandPointEntity entity = bandPointMapper.findByCode(code, orgId);
        if (entity == null) {
            return null;
        }
        BandPointDO bandPointDO = new BandPointDO();
        BeanUtils.copyProperties(entity, bandPointDO);
        return bandPointDO;
    }

    @Override
    public List<BandPointDO> findAllByPage(Integer start, Integer pageSize, String orgId) {
        List<BandPointEntity> entityList = bandPointMapper.findAllByPage(start, pageSize, orgId);
        if (entityList == null) {
            return null;
        }
        List<BandPointDO> doList = new ArrayList<>();
        for (BandPointEntity entity : entityList) {
            BandPointDO bandPointDO = new BandPointDO();
            BeanUtils.copyProperties(entity, bandPointDO);
            doList.add(bandPointDO);
        }
        return doList;
    }

    @Override
    public long countByOrgIdNotDel(String orgId) {
        Long count = bandPointMapper.countByOrgIdNotDel(orgId);
        return count == null ? 0 : count;
    }

    @Override
    public int updateDelFlagById(List idList) {
        return bandPointMapper.updateDelFlagById(DateUtil.getCommonDate(), idList);
    }

    @Override
    public void insertList(List<BandPointDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<BandPointEntity> entityList = new ArrayList<>();
        for (BandPointDO bandPointDO : list) {
            if (bandPointDO != null) {
                BandPointEntity entity = new BandPointEntity();
                BeanUtils.copyProperties(bandPointDO, entity);
                entity.setCreateTime(DateUtil.getCommonDate());
                entity.setUpdateTime(DateUtil.getCommonDate());
                entityList.add(entity);
            }
        }
        bandPointMapper.insertList(entityList);
    }
}
